<?php

   class News extends Info{
   	   
	   public function __construct(){
		parent::__construct();
	   }
	   
	   public function Index(){
	   	parent::Index();
	   }
	   
	   public function Cycle(){
	   	parent::Cycle();
	   }
	   
	   public function Add(){
	   	$this->mod = "Add";
		$categoryid = intval($_GET['categoryid']);
		$category   = DB::fetch_row("select *from `bb_category` as C left join `bb_category_info` as CI on 
		                          C.`categoryid` = CI.`categoryid` where C.`categoryid`='{$categoryid}'");
		$this->_T['category'] = $category;  //所属栏目
		$this->_T['newstime']  = date("Y-m-d H:i:s");
		$this->template("parent");
	   }
	   
	   public function Create(){
	   	$categoryid = $_POST['categoryid'];
	   	$property = implode("|",$_POST['property']);
		$objectid = File::uploadFile("titlepic", "image", "storage");
		$imgageFile = DB::fetch_row("select *from `bb_file` where `objectid`='{$objectid}'");
		$newstime = strtotime($_POST['newstime']);
	   	DB::begin();
	   	$info = array();
		$info['categoryid']    = $categoryid;
		$info['mid']       	   = 1;
		$info['userid']        = $_SESSION['U']['userid'];
		$info['createtime']    = time();
		$info['newstime']      = $newstime;
		$info['titlepic']      = $imgageFile['realname'];
		$info['headline']      = strstr($property,"headline")?1:0;
		$info['commend']       = strstr($property,"commend")?1:0;
		$infoid = DB::insert("bb_info", $info);
		if(!$infoid){
			DB::rollback();
			$this->showmessage("信息添加失败，错误代号1","goback" ,2000, "返回");
		}
		$info_info = array();
		$info_info['infoid']   = $infoid;
		$info_info['title']    = $_POST['title'];
		$info_info['ftitle']   = $_POST['ftitle'];
		$info_info['keywords'] = $_POST['keywords'];
		$info_info['smalltext']= $_POST['smalltext'];
		$info_info['writer']   = $_POST['writer'];
		$info_info['befrom']   = $_POST['befrom'];
		if(!DB::insert("bb_info_info", $info_info)){
			DB::rollback();
			$this->showmessage("信息添加失败，错误代号2","goback" ,2000, "返回");
		}
		$info_news['infoid']   = $infoid;
	   	$info_news['newstext'] = $_POST['newstext'];
		if(!DB::insert("bb_info_news", $info_news)){
			DB::rollback();
			$this->showmessage("信息添加失败，错误代号2","goback" ,2000, "返回");
		}
		DB::commit();
		$this->showmessage("信息添加成功","?m=Admin/Info/News/Index&categoryid={$categoryid}" ,2000, "返回");
	   }
	   
	   public function Edit(){
	   	$this->mod = "Edit";
		$infoid = intval($_GET['infoid']);
		$info      = DB::fetch_row("select *from `bb_info` as I left join `bb_info_info` as II on I.`infoid`=II.`infoid` where I.`infoid`='{$infoid}'");
		$info_news = DB::fetch_row("select *from `bb_info_news` where `infoid`='{$infoid}'");
		$this->_T['info'] = $info;
		$this->_T['info_news'] = $info_news;
		$categoryid = intval($_GET['categoryid']);
		$category   = DB::fetch_row("select *from `bb_category` as C left join `bb_category_info` as CI on 
		                          C.`categoryid` = CI.`categoryid` where C.`categoryid`='{$categoryid}'");
		$this->_T['category'] = $category;  //所属栏目
		$this->_T['newstime']  = date("Y-m-d H:i:s");
		$this->template("parent");
	   }
	   
	   public function Update(){
	   	$categoryid = $_POST['categoryid'];
	   	$property = implode("|",$_POST['property']);
		$objectid = File::uploadFile("titlepic", "image", "storage");
		$imgageFile = DB::fetch_row("select *from `bb_file` where `objectid`='{$objectid}'");
		$newstime = strtotime($_POST['newstime']);
		$infoid   = $_POST['infoid'];
		$titlepic = "";
		if($_POST['oldtitlepic']&&$imgageFile){
			$info = DB::fetch_row("select *from `bb_info` where `infoid`='{$infoid}'");
			File::deleteFile($info['titlepic'], "delete file");
			$titlepic = $imgageFile['realname'];
		}else if($_POST['oldtitlepic']){
			$titlepic = $_POST['oldtitlepic'];
		}else if($imgageFile){
			$titlepic = $imgageFile['realname'];
		}
        $condition = array();
		$condition['infoid'] = $infoid;
		DB::begin();
		$info = array();
		$info['newstime']      = $newstime;
		$info['titlepic']      = $titlepic;
		$info['headline']      = strstr($property,"headline")?1:0;
		$info['commend']       = strstr($property,"commend")?1:0;
		if(!DB::update("bb_info", $info, $condition)){
			DB::rollback();
			$this->showmessage("信息更新失败，错误代号1","goback" ,2000, "返回");
		}
		$info_info['title']    = $_POST['title'];
		$info_info['ftitle']   = $_POST['ftitle'];
		$info_info['keywords'] = $_POST['keywords'];
		$info_info['smalltext']= $_POST['smalltext'];
		$info_info['writer']   = $_POST['writer'];
		$info_info['befrom']   = $_POST['befrom'];
		if(!DB::update("bb_info_info", $info_info, $condition)){
			DB::rollback();
			$this->showmessage("信息更新失败，错误代号2","goback" ,2000, "返回");
		}
		$info_news['newstext'] = $_POST['newstext'];
		if(!DB::update("bb_info_news", $info_news, $condition)){
			DB::rollback();
			$this->showmessage("信息更新失败，错误代号3","goback" ,2000, "返回");
		}
		DB::commit();
		$this->showmessage("信息更新成功","?m=Admin/Info/News/Index&categoryid={$categoryid}" ,2000, "返回");
	   }
       
	   /*
	    * 软删除(单条信息)
	    */ 
	   public function Delete(){
	   	$infoid = $_GET['infoid'];
		$info = array();
		$info = DB::fetch_row("select *from `bb_info` where `infoid`='{$infoid}' and `deleted` = '1'");
		$categoryid = $info['categoryid'];
		$condition = array();
		$condition['infoid'] = $infoid;
		$data = array();
		$data['deleted'] = '1';
		if(!DB::update("bb_info", $data, $condition)){
			$this->showmessage("信息删除失败","goback" ,2000, "返回");
		}
		$this->showmessage("信息删除成功","?m=Admin/Info/News/Cycle&categoryid={$categoryid}" ,2000, "返回");
	   }
	   
	   /*
	    * 软删除（多条信息）
	    */ 
	    public function Deletes(){
	      $categoryid = $_POST['categoryid'];
	      $condition = array();
		  $condition['infoid'] = $_POST['infos'];
		  $info = array();
		  $info['deleted'] = '1';
		  if(!DB::update("bb_info",$info ,$condition)){
		  	$this->showmessage("信息批量删除失败","goback" ,2000, "返回");
		  }
		  $this->showmessage("信息批量删除成功","?m=Admin/Info/News/Cycle&categoryid={$categoryid}" ,2000, "返回");
	    }
       
	   /*
	    * 恢复（单条信息）
	    */  
	   public function Recover(){
	   	$infoid = $_GET['infoid'];
		$info = array();
		$info = DB::fetch_row("select *from `bb_info` where `infoid`='{$infoid}' and `deleted` = '1'");
		$categoryid = $info['categoryid'];
		$condition = array();
		$condition['infoid'] = $infoid;
		$data = array();
		$data['deleted'] = '0';
		if(!DB::update("bb_info", $data, $condition)){
			$this->showmessage("信息恢复失败","goback" ,2000, "返回");
		}
		$this->showmessage("信息恢复成功","?m=Admin/Info/News/Index&categoryid={$categoryid}" ,2000, "返回");
	   }
	   
	   /*
	    * 恢复（多条信息）
	    */ 
	    public function Recovers(){
	      $categoryid = $_POST['categoryid'];
	      $condition = array();
		  $condition['infoid'] = $_POST['infos'];
		  $info = array();
		  $info['deleted'] = '0';
		  if(!DB::update("bb_info", $info, $condition)){
		  	$this->showmessage("信息批量恢复失败","goback" ,2000, "返回");
		  }
		  $this->showmessage("信息批量恢复成功","?m=Admin/Info/News/Index&categoryid={$categoryid}" ,2000, "返回");
	    }
	   
	   /*
	    * 彻底删除（单条信息）
	    */
	   public function DeleteComplete(){
	   	$infoid = $_GET['infoid'];
		$info = array();
		$info = DB::fetch_row("select *from `bb_info` where `infoid`='{$infoid}' and `deleted` = '1'");
		$categoryid = $info['categoryid'];
		if(!$info){
			$this->showmessage("信息不存在","goback" ,2000, "返回");
		}
		DB::begin();
		$condition = array();
		$condition['infoid'] = $infoid;
		if(!DB::delete("bb_info", $condition)){
			DB::rollback();
			$this->showmessage("信息删除失败，错误代号1","goback" ,2000, "返回");
		}
		if(!DB::delete("bb_info_info", $condition)){
			DB::rollback();
			$this->showmessage("信息删除失败，错误代号2","goback" ,2000, "返回");
		}
		if(!DB::delete("bb_info_news", $condition)){
			DB::rollback();
			$this->showmessage("信息删除失败，错误代号3","goback" ,2000, "返回");
		}
		DB::commit();
		if($info['titlepic']){
			File::deleteFile($info['titlepic'], "delete file");
		} 
		$this->showmessage("信息删除成功","?m=Admin/Info/News/Index&categoryid={$categoryid}" ,2000, "返回");
	   }
   }
 
 

?>